package com.jrzh.mvc.controller.sys.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.config.ProjectConfigration;
import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.convert.sys.FileConvert;
import com.jrzh.mvc.model.sys.FileModel;
import com.jrzh.mvc.search.sys.FileSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.FileView;

@RestController(FileController.LOCATION +"/FileController")
@RequestMapping(FileController.LOCATION)
public class FileController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/file";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@Autowired
	ProjectConfigration projectConfigration;
	
	@PostMapping(value = "datagrid")
	@UserEvent(desc = "文件列表查询")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	public JqDataGrid<FileView> datagrid(FileSearch search) {
		JqDataGrid<FileView> dg = new JqDataGrid<FileView>();
	    try{
	    	dg = sysServiceManage.fileService.datagrid(search);
	    } catch (Exception e){
	    	e.printStackTrace();
	    }
		return dg;
	}
	
	
	@PostMapping(value = "add")
	@RequiredPermission(key =  SysActionConstants.ADD)
	@UserEvent(desc = "文件增加")
	public ResultBean add(FileView view){
		ResultBean result = new ResultBean();
		try{
			FileModel model =new FileConvert().addConvert(view);
			sysServiceManage.fileService.add(model, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.addTo_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;	
	}

	@PostMapping(value = "edit/{id}")
	@UserEvent(desc = "File修改")
	@RequiredPermission(key =  SysActionConstants.EDIT)
	public ResultBean edit(@PathVariable("id") String id, FileView view) {
		ResultBean result = new ResultBean();
		try {
			FileModel model = sysServiceManage.fileService.findById(id);
			model = new FileConvert().editConvert(view, model);
			sysServiceManage.fileService.edit(model, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.modify_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "delete/{id}")
	@UserEvent(desc = "File删除")
	@RequiredPermission(key =  SysActionConstants.DELETE)
	public ResultBean delete(@PathVariable("id") String id, FileView view) {
		ResultBean result = new ResultBean();
		try {
			FileModel model = sysServiceManage.fileService.findById(id);
			sysServiceManage.fileService.delete(model.getId(), getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.delete_success", getSessionUser()));
		} catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "deleteByUrl")
	@UserEvent(desc = "File删除")
	public ResultBean deleteByUrl() {
		ResultBean result = new ResultBean();
		try {
//			String url = request.getParameter("url");
//			log.info(url);
//			FileModel model = sysServiceManage.fileService.findByField("url", url);
//			sysServiceManage.fileService.delete(model, getSessionUser());
//			result.setStatus(ResultBean.SUCCESS);
//			result.setMsg("删除成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@Override
	protected void setData() {
		
	}

}
